import { reactive } from 'vue';

export const cart = reactive([]);

export const addToCart = (book) => {
  // 检查书籍是否已在购物车中
  const existingBook = cart.find(item => item.id === book.id);
  if (!existingBook) {
    cart.push({ ...book });
  }
};

export const removeFromCart = (bookId) => {
  const index = cart.findIndex(item => item.id === bookId);
  if (index !== -1) {
    cart.splice(index, 1);
  }
};

export const clearCart = () => {
  cart.length = 0;
};